<?php
/**
 * Created by PhpStorm.
 * User: UGLY
 * Date: 2017/12/31
 * Time: 20:06
 */

namespace app\mainiu\controller;


use think\Db;
use app\blog\controller\CommonController;
use think\Validate;

class GoodsController extends CommonController
{
    //商品表菜单
    public function goodsindex()
    {
        echo 'goodsindex';
    }
    //商品查看表
    public function goodswatch()
    {
        echo 'goodswatch';
    }
    //商品添加表
    public function goodsadd()
    {
        $data = \db('type')->where(['aid'=>$_SESSION['think']['ADMIN_ID']])->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
    //商品添加方法
    public function add()
    {
        $post = request();
        $post = $post->post();
        //要验证的字段
        $rule = [
            'name' => 'require',
            'cate' => 'require',
            'thumb'=> 'require',
            'price'=> 'require',
        ];
        $data = [
            'name'=>$post['name'],
            'des'=>htmlspecialchars_decode($post['des']),
            'cate'=>$post['cate'],
            'thumb'=>$post['thumb'],
            'price'=>$post['price'],
            'ishot'=>$post['ishot'],
            'sales'=>$post['sales'],
            'time'=>date("Y-m-d H:i:s",time()),
            'aid'=>$_SESSION['think']['ADMIN_ID'],
        ];
        //出错的信息
        $msg = [
            'name.require' => '请填写您的名称',
            'cate.require' => '请选择分类',
            'thumb.require'=> '请上传封面图',
            'price'        => '请填写价格'
        ];
        $validate = new Validate($rule,$msg);
        if($validate->check($data)){
            if(\db('goods')->insert($data)){
                echo json_encode(
                    [
                        'sta'=>1
                    ]
                );
            }else{
                echo json_encode(
                    [
                        'sta'=>2
                    ]
                );
            }
        }else{
            echo json_encode($validate->getError());
        }
    }
}